package com.crape.account.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crape.account.bo.IAccountBO;
import com.crape.common.bean.LoginUser;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.Article;
import com.crape.common.thread.ThreadObjManager;
import com.crape.common.utils.PageUtils;
import com.crape.topic.bo.IArticleBO;

@Controller
public class DarrenController {

    private IAccountBO      accountBOImpl;

    private IArticleBO ArticleBOImpl;

    @Autowired
    public void setAccountBOImpl(IAccountBO accountBOImpl) {
        this.accountBOImpl = accountBOImpl;
    }

    @Autowired
    public void setArticleBOImpl(IArticleBO ArticleBOImpl) {
        this.ArticleBOImpl = ArticleBOImpl;
    }

    @RequestMapping("/darren/darrens.jsp")
    public String all(ModelMap modelMap, Integer pageNo) {
        Long counts = accountBOImpl.countAllDarrens();
        if (counts == null || counts == 0) {
            return "/darren/darrens.html";
        }
        Long accountid = ThreadObjManager.getLoginUser().getAccountid();
        if (pageNo == null || pageNo == 0) {
            pageNo = 1;
        }
        Integer pageSize = 20;
        Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
        List<AccountInfo> darrens = accountBOImpl.getAllDarrens(accountid, startIndex, pageSize);
        if (darrens != null && darrens.size() % 2 == 1) {
            AccountInfo last = new AccountInfo();
            last.setRemark("last");
            darrens.add(last);
        }
        List<Article> hotArticles = ArticleBOImpl.getHotArticles();
        modelMap.put("darrens", darrens);
        modelMap.put("hotArticles", hotArticles);
        modelMap.put("pageNo", pageNo);
        modelMap.put("pageNumArray", PageUtils.getPagedNumArray(counts, pageSize));
        return "/darren/darrens.html";
    }

    @RequestMapping("/darren/addDarrenAttention.json")
    public ModelAndView addDarrenAttention(Long userid) {
        LoginUser loginUser = ThreadObjManager.getLoginUser();
        Long fansid = loginUser.getAccountid();
        ModelAndView mv = new ModelAndView();
        if(fansid == null){
			mv.addObject("flag", 1);
			mv.addObject("message", "请先登录或注册！");
			return mv;
		}
        try {
            accountBOImpl.addDarrenAttention(fansid, userid);
            mv.addObject("flag", 0);
            mv.addObject("message", "关注成功！");
            return mv;
        } catch (Exception e) {
        	e.printStackTrace();
        	mv.addObject("flag", 1);
            mv.addObject("message", "关注失败");
            return mv;
        }
    }

    @RequestMapping("/darren/cancelDarrenAttention.json")
    public ModelAndView cancelDarrenAttention(Long userid) {
        LoginUser loginUser = ThreadObjManager.getLoginUser();
        Long fansid = loginUser.getAccountid();
        ModelAndView mv = new ModelAndView();
        if(fansid == null){
			mv.addObject("flag", 1);
			mv.addObject("message", "请先登录或注册！");
			return mv;
		}
        try {
            accountBOImpl.cancelDarrenAttention(fansid, userid);
            mv.addObject("flag", 0);
            mv.addObject("message", "取消关注成功！");
            return mv;
        } catch (Exception e) {
        	e.printStackTrace();
        	mv.addObject("flag", 1);
            mv.addObject("message", "取消关注失败");
            return mv;
        }
    }

    @RequestMapping("/darren/articles.jsp")
    public String articles(ModelMap modelMap, Long accountid, Integer pageNo) {
        modelMap.put("account", null);
        return "/darren/articles.html";
    }

}
